package com.hd.trans.ui.adapter;


import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hd.trans.R;
import com.hd.trans.TranslationInterior;
import com.hd.trans.common.interf.Callback;
import com.hd.trans.db.bean.TranslateRecord;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:  yang
 * Date:    2020/01/07 15:58
 * Desc:    文本翻译记录适配器
 */
public class RecordTextAdapter extends BaseQuickAdapter<TranslateRecord, BaseViewHolder> {
    private boolean isManagerType;
    private boolean isChecked;//CheckBox默认值
    private Callback callback;
    private List<TranslateRecord> selectedData = new ArrayList<>();
    private List<String> selectedPositions = new ArrayList<>();
    private int longClickPosition = -1;

    public RecordTextAdapter(@Nullable List<TranslateRecord> data) {
        super(R.layout.text_home_layout, data);
    }

    public void setShowType(boolean isManagerType){
        selectedData.clear();
        selectedPositions.clear();
        this.isManagerType = isManagerType;
    }

    public boolean isManagerType() {
        return isManagerType;
    }

    public List<TranslateRecord> getSelectedData() {
        return selectedData;
    }

    public void selectAll(Callback callback){
        selectedData.clear();
        selectedPositions.clear();
        selectedData.addAll(getData());
        this.isChecked = true;
        notifyDataSetChanged();

        callback.onCallback();
    }

    public void cancelSelectAll(Callback callback) {
        selectedData.clear();
        selectedPositions.clear();
        this.isChecked = false;
        notifyDataSetChanged();

        callback.onCallback();
    }

    public void setCallbackListener(Callback callback){
        this.callback = callback;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder helper, int position) {
        final int realPosition = position - getHeaderLayoutCount();
        TranslateRecord item = getItem(position);
        if (item != null) {
            helper.setText(R.id.text_1, item.getContentOrig());
            helper.setText(R.id.text_2, item.getContentTranslate());
            helper.setTextColor(R.id.text_2, TranslationInterior.getThemeColor());
            if(isManagerType){
                final TranslateRecord itemTemp = item;
                helper.setVisible(R.id.check_box,true);
                helper.setEnabled(R.id.check_box,true);
                CheckBox checkBox = helper.getView(R.id.check_box);
                checkBox.setOnCheckedChangeListener(null);
                if(longClickPosition == realPosition){
                    selectedData.add(itemTemp);
                    selectedPositions.add(String.valueOf(longClickPosition));
                    longClickPosition = -1;
                    checkBox.setChecked(true);
                    callback.onCallback();
                }else {
                    if(selectedPositions.size() > 0 && selectedPositions.contains(String.valueOf(realPosition))){
                        checkBox.setChecked(true);
                    }else {
                        checkBox.setChecked(isChecked);
                    }
                }
                //监听
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){
                            selectedData.add(itemTemp);
                            if(!selectedPositions.contains(String.valueOf(realPosition))){
                                selectedPositions.add(String.valueOf(realPosition));
                            }
                        }else {
                            selectedData.remove(itemTemp);
                            if(selectedPositions.size() > 0){
                                selectedPositions.remove(String.valueOf(realPosition));
                            }
                        }
                        callback.onCallback();
                    }
                });
            }else {
                helper.setVisible(R.id.check_box,false);
                helper.setEnabled(R.id.check_box,false);
            }
        }
    }

    public void setItemLongClickPosition(int position) {
        longClickPosition = position;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, TranslateRecord translateRecord) {

    }
}
